package rs.networx.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.FriendRequestDao;
import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;
import rs.networx.util.ErrorMessages;

public class SearchFromRegisteredUserHomePageServlet extends HttpServlet {

	private static final long serialVersionUID = -8849285540761306158L;

	@EJB
	private PersonDao personDao;

	@EJB
	private FriendRequestDao friendRequestDao;

	private String searchData;

	private List<Person> searchResults;
	private List<Person> resultPersonsIAmNotFriendsWith;
	private List<Person> resultPersonsIAmFriendsWith;
	private List<Person> resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo;

	private Person loggedUser;

	private String username;
	private String password;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		System.out
				.println("LOADED: SearchFromRegisteredUserHomePageServlet");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());

		searchData = request.getParameter("SearchText");
		System.out.println(searchData);

		HttpSession session = request.getSession();
		loggedUser = (Person) session.getAttribute("logovaniUser");

		try {
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			System.out.println(username + "  " + password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}

		if (loggedUser == null) {
			request.getRequestDispatcher("Welcome.jsp").forward(request,
					response);
			return;
		} else if (loggedUser != null) {
			if (searchData != null && searchData != "") {
				searchResults = personDao.search(searchData);
				if (searchResults == null || searchResults.size() == 0) {
					request.setAttribute("searchStatus",
							ErrorMessages.NO_SEARCH_RESULTS);
				} else if (searchResults != null && searchResults.size() > 0) {
					request.setAttribute("searchStatus",
							ErrorMessages.THERE_IS_SEARCH_RESULTS);

					resultPersonsIAmNotFriendsWith = new ArrayList<Person>();
					resultPersonsIAmFriendsWith = new ArrayList<Person>();
					resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo = new ArrayList<Person>();

					for (int i = 0; i < searchResults.size(); i++) {
						if (searchResults.get(i) instanceof Person) {
							Person person = (Person) searchResults.get(i);
							if (loggedUser.getId().equals(person.getId())) {
								continue;
							} else {
								if (personDao.areFriends(loggedUser.getId(),
										person.getId())) {
									resultPersonsIAmFriendsWith.add(person);
								} else if (friendRequestDao
										.checkIfISentFriendRequest(loggedUser,
												person)) {
									resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo
											.add(person);
								} else {
									resultPersonsIAmNotFriendsWith.add(person);
								}
							}
						}
					}
					request.setAttribute("resultPersonsIAmFriendsWith",
							resultPersonsIAmFriendsWith);
					for (int i = 0; i < resultPersonsIAmNotFriendsWith.size(); i++) {
						System.out.println(resultPersonsIAmNotFriendsWith
								.get(i).getFirstName());
					}
					request.setAttribute("resultPersonsIAmNotFriendsWith",
							resultPersonsIAmNotFriendsWith);
					request.setAttribute(
							"resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo",
							resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo);
				}
			}
			request.getRequestDispatcher("SearchForDude.jsp").forward(
					request, response);
			return;
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}

}
